package cn.bx.bid.ctrl;

import cn.bx.bid.entity.Page;
import cn.bx.bid.entity.User;
import cn.bx.bid.service.UserService;
import cn.bx.bid.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("login")
    public ModelAndView login(HttpServletRequest request, @RequestParam("username") String name, @RequestParam("password") String pass) {
        ModelAndView mv = new ModelAndView();
        if (userService.login(name,pass) != null) {
            request.getSession().setAttribute("USER_LOGIN", name);
            mv.setViewName("welcome");
            return mv;
        }
        mv.setViewName("fail");
        request.setAttribute("msg", "用户名或密码不正确！");
        return mv;
    }

   /* public String test(HttpServletRequest request, HttpServletResponse response){
        ServletContext application = request.getSession().getServletContext();
        return "";
    }*/

   @RequestMapping("addAdmit")
    public String addAdmit(){
        return "useradd";
    }

    @RequestMapping("adminRegister")
    public ModelAndView userAdd(@ModelAttribute("u") User user) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("msg");
        if(userService.add(user) == 1){
            mv.addObject("msg","<script>alert('添加成功！');window.location = 'search.jhtml';</script>");
        }else{
            mv.addObject("msg","<script>alert('添加失败！');window.location = 'search.jhtml';</script>");
        }
        return mv;
    }

    //查找
    /*@RequestMapping("search")
    public ModelAndView search(@RequestParam(name = "pageNO",defaultValue = "1")int pageNO,@RequestParam(name = "name",required = false)String name,
                               @RequestParam(name = "startDate",required = false)Date start,@RequestParam(name = "endDate",required = false)Date end ) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("userlist");
        Page<User> page = userService.paging(pageNO,Page.PAGE_SIZE,name,start,end);
        mv.addObject("p",page);
        String url = "";
        if(name != null)
            url = url + "&&name="+name;
        if(start != null)
            url = url + "&&startDate="+StringUtil.format(start);
        if(end != null)
            url = url + "&&endDate="+StringUtil.format(end);
        mv.addObject("url",url);
        mv.addObject("name",name);
        mv.addObject("start", StringUtil.format(start));
        mv.addObject("end",StringUtil.format(end));
        return mv;
    }*/

    //修改
    @GetMapping("toUser-udp-{id}")
    public String udp(@PathVariable("id")int id, Model m){
       User u = userService.get(id);
       m.addAttribute("user",u);
       return "userudp";
    }
    @PostMapping("update")
    public ModelAndView update(@ModelAttribute("u") User user){
       LogUtil.info("管理员"+user+"修改了！");
       ModelAndView mv = new ModelAndView();
       mv.setViewName("msg");
       if(userService.update(user) == 1)
           mv.addObject("msg","<script>alert('修改成功！');window.location = 'search.jhtml';</script>");
       else
           mv.addObject("msg","<script>alert('修改失败！');window.location = 'search.jhtml';</script>");
       return mv;
    }

    //删除
    /*@GetMapping("toUser-del-{id}")
    public String del(@PathVariable(name = "id")int id, Model m){
       long[] ids = {id};
        int flag = userService.batchDel(ids);
        if(flag == 1){
            m.addAttribute("msg","<script>alert('删除成功！');window.location = 'search.jhtml';</script>");
        }else{
            m.addAttribute("msg","<script>alert('删除失败！');window.location = 'search.jhtml';</script>");
        }
        return "msg";
    }*/

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值 }
    }

    @ExceptionHandler
    public String exceptionHandler(Exception e){
        LogUtil.error("出现异常",e);
        return "error";
    }
}